package Card.connect;

import java.io.IOException;

import Card.game.Card;
import Card.game.CountPlayer;
import Card.game.Message;
import Card.game.Name;
import Card.game.Turn;

import com.lloseng.ocsf.server.ConnectionToClient;
import com.lloseng.ocsf.server.ObservableServer;

/*
 * The server that communicate with client who join this game.
 * @author Juthamas Utamaphethai 5510546964
 * @author Natcha Chidbhob 5510546026
 */
public class Server extends ObservableServer {
	/** The number of player who join the game */
	private Byte player;
	/** Name of client who connect in. */
	private Name name;
	/** The card game that control the game. */
	private Card card;
	/** keep the ID of player who recently connect. */
	private CountPlayer userid;
	/** To examine whether if the client can press the start button or not */
	private Boolean canPress;
	/** The time that client click fight or fold button. */
	private int count = 0;
	/** Number of client who press login button. */
	private int countLogin = 0;
	/** The number of round in playing game. */
	private int countRound = 0;
	/** The number of person who login. */
	private int countL = 0;
	/** Number of person who press fight button. */
	private int fight = 0;
	/** Number of person who press fold button. */
	private int fold = 0;
	/** Balance of client. */
	private int init;
	/** The bet money. */
	private int bet;
	/** Message from client. */
	private String message;
	/** Name of client. */
	private String nf;
	/** ID of person who will get all the bet. */
	private int winner;
	/** Number of person who want to play again. */
	private int countYes = 0;
	/** Turn of player. */
	private Turn turn = Turn.p1;

	/*
	 * Constructor for server.
	 * 
	 * @param port the port of this server.
	 */
	public Server(int port) {
		super(port);
		player = 0;
		canPress = false;
		name = new Name();
		card = new Card();
	}

	@Override
	// when the client conneted to the Server 1st time will come in this method
	protected void clientConnected(ConnectionToClient client) {
		player++;
		this.sendToAllClients(player);
		userid = new CountPlayer(player);
		try {
			client.sendToClient(userid);
			if (userid.getId() == 1) {
				canPress = true;
				client.sendToClient(canPress);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println("client connected " + client + " : "
				+ client.getClass());
		client.setInfo("count", "0");

	}

	/**
	 * when the client disconnect from the server will come in this method
	 */
	@Override
	protected synchronized void clientDisconnected(ConnectionToClient client) {
		System.out.println("client disconnected " + client);
		Message ms = new Message();
		ms.add(client.getInfo("name").toString());
		ms.add("has disconnected !!");
		this.sendToAllClients(ms);
		this.sendToAllClients("To Everyone -> " + client.getInfo("name")
				+ " has logout!");
		player--;
	}

	// this method will receive the msg from the client ex.
	// client.sendToServer(Object n);
	//
	@Override
	protected void handleMessageFromClient(Object msg, ConnectionToClient client) {
		// send message tell client who click to send array of bet to server.
		if (msg instanceof String[]) {
			String[] st = (String[]) msg;
			System.out.println(st[0]);
			if (st[0].equals("init")) {
				init = Integer.parseInt(st[1]);
				System.out.println("My init is " + init);
			}
			if (st[0].equals("bet")) {
				if (st[1] != "0") {
					String[] b = { "bet", st[1] };
					this.sendToAllClients(b);
				}
				bet = Integer.parseInt(st[1]);
				System.out.println("My bet is " + bet);
				card.sumBet(bet * 2);
			}
			if (st[0].equals("lose")) {
				String[] winner = { "winner", st[1] };
				this.sendToAllClients(winner);
			}

		}

		if (msg instanceof String) {
			String wo = (String) msg;
			if (client.getInfo("count").equals("0")) {
				client.setInfo("name", wo);
				name.setName(wo);
				name.add();
				this.sendToAllClients(name.getAll());
				client.setInfo("count", "1");
			} else if (wo.equals("yes")) {
				countYes++;
				if (countYes == player) {
					card.clear();
					card = new Card();
					this.sendToAllClients("clear");
					countRound = 0;
					fight = 0;
					fold = 0;
				}

			} else if (wo.equals("give name")) {
				this.sendToAllClients(name);
			} else if (wo.equals("open game")) {
				this.sendToAllClients("start");
			} else if (wo.equals("send card")) {
				if (countRound == 52 / player) {
					this.sendToAllClients("give me a money");
					System.out.println(" Give me a money!! ");
				} else if (countRound < (52 / player)) {
					card.makeAllRandom(player);
					this.sendToAllClients(card.getAllRandom());
					this.sendToAllClients(turn);
					if (turn == Turn.p1)
						turn = Turn.p2;
					else {
						turn = Turn.p1;
					}
				}

			} else if (wo.equals("login")) {
				countLogin++;
				if (countL == 0) {
					this.sendToAllClients("next login");
					countL++;
				}

				if (countLogin == player)
					this.sendToAllClients("can start");
			} else if (wo.equals("fight") || wo.equals("fold")) {
				count++;
				if (wo.equals("fold")) {
					bet = 0;
					fold++;
					nf = "" + client.getInfo("name");
				}

				if (wo.equals("fight")) {
					fight++;
				}

				if (fight == 1 && fold == 1) {
					System.out.println("Fight name is "
							+ client.getInfo("name"));
					card.clear();

					this.sendToAllClients("result");
					countRound++;
					card.setSumBet();
					count = 0;
					bet = 0;
					fight = 0;
					fold = 0;
					System.out.println("Count1 : " + count);
				} else if (count == player) {

					System.out.println("Now : " + count);
					winner = card.findWinner();
					System.out.println("get all bet" + card.getAllBet());
					int[] allBet = { winner, card.getAllBet() };
					if (fight >= 2)
						this.sendToAllClients(allBet);
					this.sendToAllClients("result");
					countRound++;
					card.setSumBet();
					fight = 0;
					fold = 0;
					bet = 0;
					count = 0;
				}

			}
		}

		if (msg instanceof Message) {
			Message ms = (Message) msg;
			message = ms.getMessage();
			ms.add(client.getInfo("name").toString());
			ms.add(message);
			System.out.println("get from " + client + ": " + message);
			this.sendToAllClients(ms);
		}
	}

	/**
	 * Make up server and then it will wait the message from client.
	 * 
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {
		Server myServer = new Server(111);
		myServer.listen();
	}
}
